let fetch = require("../../utils/fetch");
let util = require("../../utils/util");
let mta = require('../../utils/mta_analysis.js');
let app = getApp();
Page({
    data: {
        imageUrl: app.globalData.imageUrl,
        level: "00",
        avatarUrl: "",
        nickName: "小麦铺粉丝",
        winHeight: "", //窗口高度
        currentTab: 0, //预设当前项的值
        waitpaynum: 0,
        couponsNum: 0,
        my_pop: '',
        balance: 0,
        urlStr: '',
        //麦享
        maixiangType: "",
        maixaingMsg: "",
        mxUrlStr: '',
        payUrl: '',
        maixiangShow: true,
        btnShow:false,
        isLogin:false
    },
    getUserInfo: function() {
        this.setData({
            imageUrl: app.globalData.imageUrl,
            shopCode: app.globalData.common.shopCode || '',
            level: app.globalData.userInfo.level,
            avatarUrl: app.globalData.userInfo.avatarUrl,
            nickName: app.globalData.userInfo.nickName
        })
    },
    loginUrl(){
        this.setData({
            isLogin:true
        })
    },
    loginMen(){
        wx.navigateTo({
            url:"../../pages/login/login"
        });
    },
    getLevel: function() {
        fetch.loadingText(),
            fetch.getLevel().then((response) => {
                fetch.hideLoadingText()
                const res = response.data;
                if (res.code == 200) {
                    this.setData({
                        level: res.obj.level,
                        mobile: res.obj.mobile
                    })
                } else {
                    reject(res)
                }
            }).catch((e) => {
                fetch.hideLoadingText()
                fetch.showMyToast(this, e.data.des)
            })
    },

    onLoad: function(options) {
        mta.Page.init();
        mta.Event.stat("smg_wode", {})
        this.setData({
            memId: wx.getStorageSync('memberId')
        })
        if (wx.getStorageSync('flag')) {
            // this.loginUrl();
        }
    },
    onShow: function() {
        if(getApp().globalData.userInfo.memId){
          this.setData({
            btnShow:false,
            isLogin:false
          })
        }else{
            this.setData({
                btnShow:true
              })
        }

        this.getUserInfo();
        //麦享
        this.getMaiXiangInfo();
        //
        this.getConponsNum();
        this.getBalancePassword();
        

        //判断当从支付页跳转后，刷新订单
        fetch.waitPayNum().then(response => {

            this.setData({
                waitpaynum: response.data.obj
            })

            if (response.data.obj > 0) {
                console.log('设置下标')
                wx.setTabBarBadge({
                    index: 4,
                    text: String(response.data.obj)
                })
            } else {
                wx.removeTabBarBadge({
                    index: 4
                })
            }

        }).catch(error => {

        })
        // fetch.getNewCoupon()
        this.getBalance();
        this.upgrade();
        this.getLevel();
    },
    loginNo(){
        this.setData({
            isLogin:false
         })  
    },
    face: function() {

        wx.navigateTo({
            url: '../face/face',
            // url: '../balanceDetail/balanceDetail'
        })
    },
    getBalance: function() {
        // 获取余额
        fetch
            .getBalance()
            .then((response) => {
                const res = response.data;
                this.setData({
                    balance: res.obj.balance
                })
                //充值类型
                this.getPayType();
            })
            .catch((err) => {
                fetch.hideLoadingText()
                fetch.showMyToast(this, err.data.des)

            })
    },
    getConponsNum: function() {

        var _self = this;
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/coupon/couponCount.do',
            header: getApp().globalData.header,
            success: function(res) {

                if (res.data.code == 200) {
                    _self.setData({
                        couponsNum: res.data.obj.couponCount
                    })
                }

            }
        })

    },
    upgrade: function() {
        fetch.upgradeVouchers().then(response => {
            console.log('response', response)
            if (response.data.obj && response.data.obj.length > 0) {
                this.setData({
                    my_pop: 'my_pop',
                    popData: {
                        type: 'upgrade',
                        imageUrl: getApp().globalData.imageUrl,
                        obj: response.data.obj.slice(0, 3)
                    }
                })
            }
        }).catch(error => {
            console.log('error', error)

        })
    },
    closePop: function() {
        util.popClose(this)
    },
    go(e) {
        this.closePop()
        util.popGo(e)
    },
    getBalancePassword: function() {
        var _self = this;
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/balance/pwd/type.do',
            method: 'get',
            header: getApp().globalData.header,
            success: function(res) {
                wx.hideLoading();
                if (res.data.code == 200) {
                    console.log(res)
                    if (res.data.obj.type == 1) {
                        _self.setData({
                            urlStr: "/pages/balanceSet/balanceSet?souce=0" //充值密码
                        })
                    } else {
                        getApp().globalData.common.isBalance = false;
                        _self.setData({
                            urlStr: "/pages/balanceLoding/balanceLoding"
                        })
                    }

                }
            }

        })

    },
    getMaiXiangInfo: function() {
        // if (!getApp().globalData.common.shopCode) {
        //     this.setData({
        //         maixiangShow: false,
        //         maixaingMsg: "请扫描门店，开通麦享会员"
        //     })
        //     return
        // }
        var _self = this;
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/maixiang/checkMaiXiang.do',
            data: {

                shopCode: getApp().globalData.common.shopCode || '',
            },
            header: getApp().globalData.header,
            success: function(res) {
                if (res.data.code == 200) {
                    console.log(res.data.obj)
                    if (res.data.obj.flag == 1) {
                        _self.getMXsaveMoney();

                    } else if (res.data.obj.flag == 2) {
                        getApp().globalData.common.mxShopCode = getApp().globalData.common.shopCode;
                        _self.setData({
                            mxUrlStr: "/pages/maixiang/maixiang",
                            maixaingMsg: "开通麦享会员，购物" + res.data.obj.discount * 100 / 10 + "折"
                        })
                    } else if (res.data.obj.flag == 3) {
                        getApp().globalData.common.mxShopCode = null;
                        _self.setData({
                            mxUrlStr: "/pages/maixiang/maixiang",
                            maixaingMsg: "开通麦享，享受更多优惠"
                        })
                    }
                } else {
                    //非200
                }
            },
            fail: function() {
                //  console.log(12222);
            }
        })
    },
    getMXsaveMoney: function() {
        var _self = this;
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/maixiang/getTotalDiscountAmount.do',
            data: {
                shopCode: getApp().globalData.common.mxShopCode || '',
            },
            header: getApp().globalData.header,
            success: function(res) {
                if (res.data.code == 200) {

                    _self.setData({
                        mxUrlStr: "/pages/maixiangMember/maixiangMember",
                        maixaingMsg: "麦享会员已为您节省" + res.data.obj.totalDiscountAmount + '元'
                    })

                } else {

                }
            }
        })
    },
    getPayType: function() {
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        var _self = this;
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/recharge/getReturnPage.do',
            data: {
                shopCode: getApp().globalData.common.shopCode || '',
            },
            header: getApp().globalData.header,
            success: function(res) {
                wx.hideLoading();
                console.log(res)
                if (res.data.code == 200) {
                    if (!res.data.obj.result) {
                        _self.setData({
                            payUrl: '/pages/recharge/recharge?balance=' + _self.data.balance
                        })
                    } else {
                        _self.setData({
                            payUrl: '/pages/newRecharge/newRecharge?balance=' + _self.data.balance + '&id=' + res.data.obj.activityCode
                        })
                    }
                }
            }
        })


    }

})